package com.manager.car.util;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.util.MailSSLSocketFactory;
public class Email {

	public static void SendMsg(String pwd,String email) throws Exception{
		Properties props = new Properties();//创建属性对象设置属性
		 
	    // 开启debug调试
	    props.setProperty("mail.debug", "true");
	    // 发送服务器需要身份验证
	    props.setProperty("mail.smtp.auth", "true");
	    // 设置邮件服务器主机名
	    props.setProperty("mail.host", "smtp.qq.com");
	    // 发送邮件协议名称
	    props.setProperty("mail.transport.protocol", "smtp");
	    //加密处理
	    MailSSLSocketFactory sf = new MailSSLSocketFactory();
	    sf.setTrustAllHosts(true);
	    props.put("mail.smtp.ssl.enable", "true");
	    props.put("mail.smtp.ssl.socketFactory", sf);
	 
	    Session session = Session.getInstance(props);
		Message message = new MimeMessage(session);//创建信息发送内容对象
		
		message.setSubject("汽车管理系统零时密码");//设置标题
		
		message.setText("你本次登录汽车管理系统的验证码为："+pwd+",若非本人操作，请忽略！");//发送的类容
		
		message.setFrom(new InternetAddress("2118815147@qq.com"));//发件人
		
		message.setRecipient(RecipientType.TO, new InternetAddress(email));//收件人
		
		Transport tran = session.getTransport();//创建发送消息的对象
		//连接rldacdrdpcajcijc在邮件想中开发模式寻找
		tran.connect("smtp.qq.com", "2118815147@qq.com", "rldacdrdpcajcijc");
		
		tran.sendMessage(message, message.getAllRecipients());//发送消息
		tran.close();//关闭
		
		System.out.println("发送成功");
	}
}
